﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.WPF;
using System.Windows;
using CSharp.Libs.Koc.KernelModels;

namespace CSharp.Libs.Koc.KernelViewModels
{
    public class SoftwareAuthorizedViewModel : ViewModelBase
    {
        #region declarations
        private Kernel _KRNL = null;

        private Boolean _fLabelVisible = true;
        private String[] _aryLabelTexts = { "No Connection yet", "No Game Data received yet", "No Server Number received yet", "You are not authorized to use this software. Sorry.", "Your are authorized. Congratulations. :)" };
        private LabelTextLevel _ltlLabelTextIndex = LabelTextLevel.NoConnection;
        private enum LabelTextLevel
        {
            NoConnection = 0,
            NoGameData = 1,
            NoServerNumber = 2,
            NotAuthorized = 3,
            Authorized = 4
        }
        #endregion
        


        #region constructor / finalizer
        public SoftwareAuthorizedViewModel(
            Kernel KRNL)
        {
            _KRNL = KRNL;

            _KRNL.TM.ReceivingEventsChanged += new TrafficMonitor.ReceivingEventsChangedEventHandler(TM_ReceivingEventsChanged);
            _KRNL.TM.SeedReceived += new TrafficMonitor.SeedReceivedEventHandler(TM_SeedReceived);
            _KRNL.TM.KocServerChanged += new TrafficMonitor.KocServerChangedEventHandler(TM_KocServerChanged);
            _KRNL.TM.UserIsValidChanged += new TrafficMonitor.UserIsValidChangedEventHandler(TM_UserIsValidChanged);
        }

        ~SoftwareAuthorizedViewModel()
        {
            _KRNL = null;
        }
        #endregion
        
        
        
        #region event handling
        void TM_ReceivingEventsChanged(object sender)
        {
            _CheckVisible();
        }

        void TM_SeedReceived(object sender, SeedReceivedEventArgs srea)
        {
            _CheckVisible();
        }

        void TM_KocServerChanged(object sender)
        {
            _CheckVisible();
        }

        void TM_UserIsValidChanged(object sender)
        {
            _CheckVisible();
        }
        #endregion
        
        
        
        #region private functions
        /// <summary>_CheckVisible</summary>
        /// <history>29.07.2011</history>
        private void _CheckVisible()
        {
            try
            {
                if(_KRNL.TM.ReceivingEvents &&
                    _ltlLabelTextIndex <= LabelTextLevel.NoConnection)
                {
                    _ltlLabelTextIndex = LabelTextLevel.NoGameData;
                }

                if(_KRNL.TM.SeedCount > 0 &&
                    _ltlLabelTextIndex <= LabelTextLevel.NoGameData)
                {
                    _ltlLabelTextIndex = LabelTextLevel.NoServerNumber;
                }

                if(Int32.Parse(_KRNL.TM.CurrentServerId) > 0 &&
                    _ltlLabelTextIndex <= LabelTextLevel.NoServerNumber)
                {
                    _ltlLabelTextIndex = LabelTextLevel.NotAuthorized;
                }

                if(_KRNL.TM.UserIsValid &&
                    _ltlLabelTextIndex <= LabelTextLevel.NotAuthorized)
                {
                    _ltlLabelTextIndex = LabelTextLevel.Authorized;
                }

                if(_KRNL.TM.UserIsValid)
                {
                    _fLabelVisible = false;
                }
                else
                {
                    _fLabelVisible = true;
                }

                OnPropertyChanged("LabelVisible");
                OnPropertyChanged("LabelText");
            }
            catch (Exception ex)
            {
                _KRNL.EC.AddException(ex, this.ToString(), _KRNL.EC.Tools.GetCurrentMethodName());
                _fLabelVisible = true;
            }
        }
        #endregion
        
        
        
        #region interface
        #region properties
        public Visibility LabelVisible
        {
            get
            {
#if DISABLE_AUTHORIZATION_LABEL
                _fLabelVisible = false;
#endif
                if (_fLabelVisible)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Hidden;
                }
            }
        }

        public String LabelText
        {
            get
            {
                return _aryLabelTexts[_ltlLabelTextIndex.GetHashCode()];
            }
        }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
